package top.timous;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;
import top.timous.Interceptor.LoginInterceptor;

import java.util.ArrayList;
import java.util.List;

@ComponentScan(basePackages = "top.timous.controller")
@Configuration
@EnableWebMvc // 表明这是一个webmvc控制器
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;
    /**
     * 当url请求没有匹配到后台的方法的时候，定位到一个静态页面
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();//给静态页面资源方向
    }

    /**
     * 设置默认的静态资源访问路径
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("/login");
    }

    /**
     *给视图添加前缀和后缀
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/" , ".jsp");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> list = new ArrayList<String>();
        list.add("/user/login");
        list.add("/");
        list.add("/index");
        list.add("/epidemicData/ajax/lastestData");
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(list);
    }
}
